(function (angular) {
    angular.module('movie.services.http', [])
        .service('HttpService', ['$window', function ($window) {
            $window.callbacks = {};//解决污染全局作用域的问题
            this.jsonp = function crossDomain(url, params, fn) {
                let cbName = `jsonp_${Math.random().toString().substr(2)}`;
                $window[cbName] = function (data) {
                    fn(data);
                    $window.document.body.removeChild(scriptElement);
                };
                let queryString = '';
                for (let key in params) {
                    queryString += `${key}=${params[key]}&`;
                }
                queryString += `callback=${cbName}`;
                url = `${url}?${queryString}`;
                let scriptElement = $window.document.createElement('script');
                scriptElement.src = url;
                $window.document.body.appendChild(scriptElement);
            }
        }])
})(angular);
